package com.spzx.product.controller;

import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.product.domain.Product;
import com.spzx.product.service.IProductService;
import com.spzx.product.service.IProductUnitService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import com.spzx.common.core.web.controller.BaseController;

import java.util.List;

/**
 * <p>
 * 商品 前端控制器
 * </p>
 *
 * @author Login
 * @since 2025-07-17
 */
@Tag(name="商品列表")
@RestController
@RequestMapping("/product")
public class ProductController extends BaseController {

    @Autowired
    private IProductService productService;

    @Operation(summary = "查询商品列表")
    @GetMapping("/list")
    public TableDataInfo list(Product product) {
        startPage();
        List<Product> list = productService.selectProduct(product);
        return getDataTable(list);
    }


    @Operation(summary = "新增商品")
    @PostMapping
    public AjaxResult insertProduct(
            @Parameter(description = "商品信息")
            @RequestBody Product product
    ){
        return toAjax(productService.insertProduct(product));
    }

    @Operation(summary = "更新上下架状态")
    @GetMapping("/updateStatus/{id}/{status}")
    public AjaxResult updateStatus(
            @Parameter(description = "商品id") @PathVariable Long id,
            @Parameter(description = "上下架状态") @PathVariable Integer status
    ){
        if (status.intValue() != 1 && status.intValue() != -1){
            return AjaxResult.error("状态值错误");
        }
        return toAjax(productService.updateStatus(id, status));

    }
    @Operation(summary = "删除商品")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(
            @Parameter (description = "商品id列表")
            @PathVariable List<Long> ids){
        return toAjax(productService.removeProduct(ids));
    }

    @Operation(summary = "获取商品详细信息")
    @GetMapping("/{id}")
    public AjaxResult getInfo(
            @PathVariable Long id
    ){
        return success(productService.selectProductById(id));
    }
    @Operation(summary = "修改商品")
    @PutMapping
    public AjaxResult edit(@RequestBody Product product) {
        return toAjax(productService.updateProduct(product));
    }

}
